package com.example.administrator.ac;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;

import android.support.annotation.IdRes;
import android.util.Log;
import android.view.View;

import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;


public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener{
    public static Context mcontext;
    private MyFragment funprocess;
    private MyFragment fcareworker;
    private MyFragment fhistory;
    private FragmentManager fManager;
    private RadioGroup rdg;
    private RadioButton rbt_unprocess;
    private RadioButton rbt_history;
    private RadioButton rbt_careworker;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();//初始化组件



    }

    private void init() {
        mcontext=this;
        fManager = getFragmentManager();
        rdg=(RadioGroup)findViewById(R.id.rdg);
        rbt_unprocess = (RadioButton) findViewById(R.id.rbt_unprocess);
        rbt_careworker = (RadioButton) findViewById(R.id.rbt_careworker);
        rbt_history = (RadioButton) findViewById(R.id.rbt_history);
       rdg.setOnCheckedChangeListener(this);
        rbt_unprocess.setChecked(true);

    }


//隐藏不显示的Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(funprocess != null) fragmentTransaction.hide(funprocess);
        if(fhistory != null)fragmentTransaction.hide(fhistory);
        if(fcareworker != null)fragmentTransaction.hide(fcareworker);
        
    }


    //根据点击选择对应的Fragment
    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);

        switch(checkedId){
            case R.id.rbt_unprocess:


                if(funprocess==null){
                    funprocess=new MyFragment(R.layout.unprocessorderpage);

                    fTransaction.add(R.id.ly_content, funprocess);

                }
                else{
                    fTransaction.show(funprocess);
                }
                break;

            case R.id.rbt_careworker:


                if(fcareworker==null){
                    fcareworker=new MyFragment(R.layout.careworkerpage);
                    fTransaction.add(R.id.ly_content, fcareworker);
                }
                else{
                    fTransaction.show(fcareworker);
                }
                break;

            case R.id.rbt_history:


                if(fhistory==null){
                    fhistory=new MyFragment(R.layout.historyorderpage);
                    fTransaction.add(R.id.ly_content, fhistory);
                }
                else{
                    fTransaction.show(fhistory);
                }
                break;
        }

        fTransaction.commit();


    }
}
